package com.test;

import com.entity.User;
import com.mapper.UserMapper;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import org.junit.Test;
import org.junit.runner.RunWith;


import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.ResourceBundle;


public class Mybatis_test {

    @Test
    public void testFindUserList(){
        // 加载核心配置文件(路径相对于resources目录)
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
// 获得sqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
// 获得sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
// 执行sql语句 参数：namespace+id
        List<User> userList = sqlSession.selectList("userMapper.findAll");
// 打印结果
        System.out.println(userList);
// 释放资源
        sqlSession.close();
    }

}
